
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for view_all_content_items.
	''' </summary>
	Public Partial Class view_all_content_items
		Inherits BaseWebPage
		Protected pageID As Integer = -1
		Protected usr As User
		Protected litMusicTracks As System.Web.UI.WebControls.Literal
		Protected htPageConstants As New Hashtable()
		Protected period As DateTime = DateTime.Now

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here

			usr = LoggedUser

			Me.initPageTitles(Constants.VIEW_ALL_CONTENT_ITEMS_PAGE, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			If Constants.SHOW_MUSIC <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			' init special page
			If Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM) = Nothing OrElse Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM) = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			Try
				pageID = Convert.ToInt32(Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM))
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End Try

			If pageID = 0 Then
				Dim music As BO.SpecialPage = BO.SpecialPages.GetSpecialPageByName(Constants.MUSIC_PAGE)
				If music <> Nothing Then
					pageID = music.ID
				End If
			End If

			Dim CurrentPage As SpecialPage = SpecialPages.GetSpecialPageByID(pageID)

			If CurrentPage = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			Me.initPageTitles(Constants.SPECIAL, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			' init tabs

			ucMusicTabs.ActiveTab = 4
			ucMusicTabs.PageName = Constants.VIEW_ALL_CONTENT_ITEMS_PAGE

			' init page title
			litPageName.Text = CurrentPage.Name

			If usr <> Nothing AndAlso usr.Status = Constants.appAdmin Then
				hlAddContentItem.NavigateUrl = "../" + Constants.ADD_CONTENT_ITEM + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID
			Else
				hlAddContentItem.Visible = False
			End If

			If Request.Params("p") <> Nothing AndAlso Request.Params("p") <> "" Then
				Dim ppTemp As String = Request.Params("p").ToString()
				Dim mandyear As String() = ppTemp.Split("_"C)
				Dim yearID As Integer = Convert.ToInt32(mandyear(0))
				Dim monthID As Integer = Convert.ToInt32(mandyear(1))

					' for last month always show top N items, for all other months all items for the month
					'DateTime tempPeriod = new DateTime(yearID, quarterID, 1);
					'if( tempPeriod < period)
					'	period = tempPeriod;
				period = New DateTime(yearID, monthID, 1)
			Else
					'period = Common.DateUtilities.GetStartOfQuarter( period.Year, Common.DateUtilities.GetQuarter((Common.Month)period.Month )) ;
				period = Common.DateUtilities.GetStartOfCurrentQuarter()
			End If

			If Not Me.IsPostBack Then
				FillQuartersDropDown()
				BindGrid()
			End If
		End Sub

		Protected Sub BindGrid()
			'init datagrid with database data ordered param
			dgContentItems.DataSource = CreateDataSource()
			dgContentItems.DataBind()
		End Sub

		Protected Function CreateDataSource() As ICollection
			Dim contentItems As ArrayList = SpecialPage.GetContentItems(pageID, period, -1, -1, -1)
			'DateTime.MinValue, -1, -1, -1);
			Dim dt As New DataTable()

			Dim contentItemsCount As Integer = contentItems.Count
			dgContentItems.AllowSorting = True

			If contentItemsCount <= 0 Then
				lblNoContentItems.Visible = True
				dgContentItems.Visible = False
			Else
				Dim dr As DataRow
				dt.Columns.Add(New DataColumn("MemberName", GetType(String)))
				dt.Columns.Add(New DataColumn("MemberImageURL", GetType(String)))
				dt.Columns.Add(New DataColumn("URL", GetType(String)))
				dt.Columns.Add(New DataColumn("ItemID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("ItemURL", GetType(String)))
				dt.Columns.Add(New DataColumn("ItemName", GetType(String)))
				dt.Columns.Add(New DataColumn("ItemGenreName", GetType(String)))
				dt.Columns.Add(New DataColumn("EditItemLinkVisibility", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("EditItemLink", GetType(String)))
				dt.Columns.Add(New DataColumn("OnClickJS", GetType(String)))

				' List left items
				Dim user As User = Nothing
				Dim pic As UserPicture
				Dim grp As Group = Nothing
				Dim grpPic As GroupPicture
				Dim tmpLinkType As New LinkTypes.LinkType(-1, "")

				For Each result As ContentItem In contentItems
					dr = dt.NewRow()

					If result.UserID > 0 Then
						user = BO.Users.GetUserByID(result.UserID)
						dr(0) = HttpUtility.HtmlEncode(user.Name)
						pic = user.DefaultPicture
						If pic <> Nothing Then
							dr(1) = Pictures.GetPictureSrcFullURL(pic, True, "../")
						Else
							dr(1) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If

						dr(2) = Users.ComposeMemeberLink(user)
						dr(9) = Users.ComposeMemeberLinkOnClickJS(user, trUserCanceledMessage)
ElseIf result.GroupID > 0 Then
						grp = BO.Groups.GetGroupByID(result.GroupID)
						dr(0) = HttpUtility.HtmlEncode(grp.Name)
						grpPic = grp.DefaultPicture
						If grpPic <> Nothing Then
							dr(1) = Pictures.GetPictureSrcFullURL(grpPic, True, "../")
						Else
							dr(1) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If
						dr(2) = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID
					End If
					dr(3) = result.ID
					dr(4) = "../" + Constants.VIEW_CONTENT_ITEM + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&id=" + result.ID
					dr(5) = HttpUtility.HtmlEncode(result.LinkName)
					If result.LinkName = "" AndAlso result.LinkURL <> "" Then
						dr(5) = result.LinkURL
					End If

					dr(6) = Me.getBeginning(result.Content, Constants.MAX_CONTENT_ITEM_DESC_LENGTH)
					dr(7) = False

					If usr <> Nothing AndAlso usr.Status = Constants.appAdmin Then
						dr(7) = True
					End If
					dr(8) = "../" + Constants.ADD_CONTENT_ITEM + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID + "&id=" + result.ID

					dt.Rows.Add(dr)
				Next
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Private Sub dgContentItems_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim lbPushed As LinkButton = DirectCast(e.CommandSource, LinkButton)
			Dim cmd As String = e.CommandName
			If cmd = "DeleteItem" Then
				Dim contentItem As ContentItem = ContentItem.GetContentItemByID(Convert.ToInt32(e.CommandArgument))
				If contentItem <> Nothing Then
					contentItem.DoDelete()
				End If
			End If

			Response.Redirect(Constants.VIEW_ALL_CONTENT_ITEMS_PAGE + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageID)
		End Sub

		Protected Sub FillQuartersDropDown()
			ddlQuarters.ClearSelection()

			Dim i As Integer = 0
			While i < Constants.MAX_QUARTER
				Dim startPeriod As DateTime = Common.DateUtilities.GetStartOfQuarter(DateTime.Now.AddMonths(-3 * i).Year, Common.DateUtilities.GetQuarter(DirectCast(DateTime.Now.AddMonths(-3 * i).Month, Common.Month)))
				Dim endPeriod As DateTime = Common.DateUtilities.GetEndOfQuarter(DateTime.Now.AddMonths(-3 * i).Year, Common.DateUtilities.GetQuarter(DirectCast(DateTime.Now.AddMonths(-3 * i).Month, Common.Month)))
				ddlQuarters.Items.Add(New ListItem(String.Format("{0} - {1}", startPeriod.ToString("MMM, yyyy"), endPeriod.ToString("MMM, yyyy")), String.Format("{0}_{1}", startPeriod.Year.ToString(), startPeriod.Month)))
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While

			ddlQuarters.SelectedIndex = ddlQuarters.Items.IndexOf(ddlQuarters.Items.FindByValue(String.Format("{0}_{1}", period.Year.ToString(), period.Month)))

		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dgContentItems.ItemCommand, AddressOf Me.dgContentItems_ItemCommand
		End Sub
#End Region

		Protected Sub ddlQuarters_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
			Response.Redirect(String.Format("{0}?{1}={2}&p={3}", Constants.VIEW_ALL_CONTENT_ITEMS_PAGE, Constants.SPECIAL_PAGE_TYPE_ID_PARAM, pageID, ddlQuarters.SelectedValue))
			'AutoPostBack="false"
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






